function whenReady (callback) {
	var addListener = document.addEventListener || document.attachEvent,
		removeListener =  document.removeEventListener || document.detachEvent,
		eventName = document.addEventListener ? "DOMContentLoaded" : "onreadystatechange";

	addListener.call(document, eventName, function () {
		removeListener.call(document, eventName, arguments.callee, false);
		callback();
	}, false);
}


function removeRequiredAttributes(elements) {
	for (var i=0; i<elements.length; i++) {
		var element = elements[i];
		element.removeAttribute('required');
	}
}

function addRequiredAttributes(elements) {
	for (var i=0; i<elements.length; i++) {
		var element = elements[i];
		element.setAttribute('required', 'required');
	}
}

whenReady(function () {

	if (!document.querySelector ||
		!document.addEventListener ||
		!document.getElementById('old-browser').classList) {

		document.getElementById('old-browser').className = document.getElementById('old-browser').className.replace(/(?:^|\s)hidden(?!\S)/g, '');
	}


	Element.prototype.lookupParent = function (tagName) {
		var el = this;
		do {
			el = el.parentNode;
		} while (el.tagName.toLowerCase() != tagName.toLowerCase());
		return (el.tagName.toLowerCase() == tagName.toLowerCase()) ? el : null;
	};


	NodeList.prototype.addEventListener = function (event, callback, useCapture) {
		for (var i = 0; i < this.length; ++i) {
			this[i].addEventListener(event, callback, useCapture);
		}
	};


	document.querySelectorAll('.confirm').addEventListener('click', function (e) {
		if (!window.confirm(this.getAttribute('data-confirm'))) {
			e.preventDefault();
		}
	}, false);

	if (printBtn = document.querySelector('#btn-print')) {
		printBtn.addEventListener('click', function (e) {
			print();
			e.preventDefault();
		}, false);
	}

	if (clearFilter = document.querySelector('#frm-filter-clear')) {
		clearFilter.addEventListener('click', function (e) {
			document.querySelector('#frm-filter-subject').value = '';
			document.querySelector('#frm-filter-from').value = '';
			document.querySelector('#frm-filter-to').value = '';
			document.querySelector('#frm-filter').submit();
			e.preventDefault();
		}, false);
	}

	document.querySelector('#btn-add').addEventListener('click', function (e) {
		var rows = document.querySelectorAll('.item');
		for (var i = 0; i < rows.length; ++i) {
			rows[i].classList.remove('hidden');
		}

		var editRows = document.querySelectorAll('.frm-edit');
		for (var i = 0; i < editRows.length; ++i) {
			editRows[i].classList.add('hidden');
			removeRequiredAttributes(editRows[i].querySelectorAll('[required]'));
		}

		this.lookupParent('tr').classList.add('hidden'); // tr > td > #btn-add
		document.querySelector('#frm-add').classList.remove('hidden');

		addRequiredAttributes(document.querySelectorAll('#frm-add [data-required]'));

		e.preventDefault();
	}, false);

	document.querySelectorAll('.icon-edit').addEventListener('click', function (e) {
		document.querySelector('#row-add').classList.remove('hidden');
		var rows = document.querySelectorAll('.item');
		for (var i = 0; i < rows.length; ++i) {
			rows[i].classList.remove('hidden');
		}

		var row = this.lookupParent('tr');
		var id = row.getAttribute('data-id');
		row.classList.add('hidden');

		document.querySelector('#frm-add').classList.add('hidden');
		removeRequiredAttributes(document.querySelectorAll('#frm-add [required]'));

		var editRows = document.querySelectorAll('.frm-edit');
		for (var i = 0; i < editRows.length; ++i) {
			editRows[i].classList.add('hidden');
			removeRequiredAttributes(editRows[i].querySelectorAll('[required]'));
		}

		document.querySelector('#frm-edit-' + id).classList.remove('hidden');
		addRequiredAttributes(document.querySelectorAll('#frm-edit-' + id + ' [data-required]'));

		e.preventDefault();
	});

	document.querySelectorAll('.icon-cancel').addEventListener('click', function (e) {
		var row = this.lookupParent('tr');
		var id = row.getAttribute('data-id');
		row.classList.add('hidden');

		removeRequiredAttributes(row.querySelectorAll('[required]'));

		document.querySelector('#item-' + id).classList.remove('hidden');

		e.preventDefault();
	}, false);

});
